<?php
include 'config.php';
$con=mysqli_connect(HOST,USER,PASS);
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
echo "<br>Attempting to create DB</br>";
// Create database
$sql="CREATE DATABASE IF NOT EXISTS " .DBNAME;
$DBcreated=mysqli_query($con,$sql);
mysqli_close($con);
if ($DBcreated)
  {
	echo "<br>DB Created</br>";
	echo "<br>Attempting to create tables</br>";
	$con=mysqli_connect(HOST,USER,PASS,DBNAME);
	$sql= "CREATE TABLE IF NOT EXISTS results 
	(PID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(PID), ";
	
	
	$dbdef = simplexml_load_file(DBDEF);
	//print_r($xml);
	//echo $xml->DBName;
	$FieldArray=$dbdef->Fields->Field;
	$NumOfFields=sizeof($FieldArray);
	$i=0;
	foreach ($FieldArray as $Field)
		{
			$sql=$sql . $Field->Name . " " .$Field->Type;
			$i++;
			if ($i<$NumOfFields)
				{
				$sql.=", ";
				}
		}
	$sql.=")";
	echo $sql;
	$TableCreated=mysqli_query($con,$sql);

	if ($TableCreated)
		{
			echo "<br>Table Created</br>";
		}
	else
		{
			echo "<br>Error creating tables</br>";
			echo $TableCreated;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
  }
else
  {
  echo "Error creating database: " . mysqli_error();
  }
?>